package com.harmonk.firstscene.ui.component

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.harmonk.firstscene.constants.Utils
import com.harmonk.firstscene.db.entity.TrafficEvent
import androidx.compose.foundation.combinedClickable
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Button
import androidx.compose.material3.TextButton
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.foundation.ExperimentalFoundationApi
import com.harmonk.firstscene.db.entity.Road

/**
 * 显示TrafficEvent对象简要信息的组件
 * 包括时间、位置等信息
 */
@OptIn(ExperimentalFoundationApi::class)
@Composable
fun EventBrief(
    trafficEvent: TrafficEvent,
    road: Road?,
    modifier: Modifier = Modifier,
    onEventClick: ((TrafficEvent) -> Unit)? = null,
    onDeleteEvent: ((TrafficEvent) -> Unit)? = null,
) {
    var showDeleteDialog by remember { mutableStateOf(false) }

    if (showDeleteDialog) {
        AlertDialog(
            onDismissRequest = { showDeleteDialog = false },
            title = { Text("确认删除") },
            text = { Text("确定要删除这条记录及其相关信息吗？此操作不可撤销。") },
            confirmButton = {
                Button(
                    onClick = {
                        onDeleteEvent?.invoke(trafficEvent)
                        showDeleteDialog = false
                    }
                ) {
                    Text("确认")
                }
            },
            dismissButton = {
                TextButton(
                    onClick = { showDeleteDialog = false }
                ) {
                    Text("取消")
                }
            }
        )
    }

    Card(
        modifier = modifier
            .fillMaxWidth()
            .padding(horizontal = 12.dp)
            .then(
                if (onEventClick != null) {
                    Modifier.combinedClickable(
                        onClick = { onEventClick(trafficEvent) },
                        onLongClick = {
                            if (onDeleteEvent != null) {
                                showDeleteDialog = true
                            }
                        }
                    )
                } else {
                    Modifier
                }
            ),
        shape = MaterialTheme.shapes.medium
    ) {
        Column(
            modifier = Modifier
                .fillMaxWidth()
                .padding(8.dp)
        ) {
            // 时间信息
            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(bottom = 4.dp)
            ) {
                Text(
                    text = Utils.TimeFormatter.autoFormat(trafficEvent.timestamp),
                    style = MaterialTheme.typography.bodyMedium,
                    color = MaterialTheme.colorScheme.primary
                )
            }

            // 位置信息
            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(bottom = 4.dp)
            ) {
                Text(
                    text = Utils.locationFormat(
                        road,
                        trafficEvent.direction,
                        trafficEvent.isMainLine,
                        trafficEvent.kilometer,
                        trafficEvent.meter,
                        trafficEvent.location
                    ),
                    style = MaterialTheme.typography.bodyMedium
                )
            }

            // 简要信息
            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(bottom = 4.dp)
            ) {
                Text(
                    text = trafficEvent.brief,
                    style = MaterialTheme.typography.bodyMedium
                )
            }

            // 损伤情况
            Row(
                modifier = Modifier
                    .fillMaxWidth()
            ) {
                val damageTypes = mutableListOf<String>()

                if (trafficEvent.haveVehicleDamage) {
                    damageTypes.add("车损")
                }

                if (trafficEvent.haveInjuries) {
                    damageTypes.add("人伤")
                }

                if (trafficEvent.haveRoadPropertyLoss) {
                    damageTypes.add("路产")
                }

                if (trafficEvent.haveGoodsLoss) {
                    damageTypes.add("货损")
                }

                if (trafficEvent.haveRescueExpenses) {
                    damageTypes.add("施救费")
                }

                Text(
                    text = damageTypes.joinToString(" "),
                    style = MaterialTheme.typography.bodyMedium,
                    color = MaterialTheme.colorScheme.error
                )
            }
        }
    }
}

@Preview(showBackground = true)
@Composable
fun EventBriefPreview() {
    val sampleEvent = TrafficEvent(
        eventId = 1L,
        timestamp = System.currentTimeMillis(),
        location = "京沪高速北京段",
        brief = "两车追尾事故",
        haveVehicleDamage = true,
        haveInjuries = false,
        haveRoadPropertyLoss = false,
        haveGoodsLoss = false,
        haveRescueExpenses = false
    )
    val sampleRoad = Road(roadId = 1L, name = "京沪高速", upDirection = "北京", downDirection = "上海")

    EventBrief(trafficEvent = sampleEvent, road = sampleRoad)
}